﻿<script type="text/javascript">
    function initcombo() {
        //选择卡号取出相对应的司机工号，姓名，所属分中心，分站，卡内金额
        $("#CardCode").combobox({
            url: '@Url.Content("~/CM/CardInfo/GetYXCardCode?em=")' + '@ViewData["em"]' + '&selfCenterID=' + '@ViewData["selfCenterID"]' + '&selfStationID=' + '@ViewData["selfStationID"]' + '&selfWorkCode=' + '@ViewData["selfWorkCode"]',
            valueField: 'Name',
            textField: 'Name',
            editable: true,
            prompt: '--请选择--',
            onSelect: function (rec) {
                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/CM/Recharge/GetListByCardCode")',
                    data:
                        {
                            cardCode: rec.Name
                        },
                    dataType: "json",
                    success: function (jsData) {

                        $('#DriverWorkID').combobox('setText', jsData.model.DriverWorkID);
                        $('#DriverName').combobox("setText", jsData.model.DriverName);
                        $('#Center').combobox("setValue", jsData.model.CenterID);
                        $('#Station').combobox("setValue", jsData.model.StationID);
                        $('#Center').combobox("setText", jsData.model.Center);
                        $('#Station').combobox("setText", jsData.model.Station);
                        $('#Remaining').numberbox("setText", jsData.model.bak1);
                    }
                });
            },
            onChange: function () {
                $('#DriverWorkID').combobox('clear');
                $('#DriverName').combobox('clear');
                $('#Center').combobox('clear');
                $('#Station').combobox('clear');
                $('#Remaining').numberbox('clear');
            }
        });
        //充值日期不能超过当前日期
        $("#RechargeTime").datetimebox({
            onChange: function () {
                var t1 = $("#RechargeTime").datetimebox("getValue");
                t1 = t1.replace(/-/g, "/");
                var da = new Date(t1);
                var MM = da.getMonth() < 9 ? "0" + (parseInt(da.getMonth()) + 1) : (parseInt(da.getMonth()) + 1);
                var dd = da.getDate() < 10 ? "0" + da.getDate() : da.getDate();
                var hh = da.getHours() < 10 ? "0" + da.getHours() : da.getHours();
                var mm = da.getMinutes() < 10 ? "0" + da.getMinutes() : da.getMinutes();
                var ss = da.getSeconds() < 10 ? "0" + da.getSeconds() : da.getSeconds();
                var t3 = da.getFullYear() + "-" + MM + "-" + dd + "  " + hh + ":" + mm + ":" + ss;


                var t2 = $.GetToday();
                if (t3 > t2) {
                    $.messager.alert('提示', '充值日期超过当前时间，请重新填写', 'info', function () {
                        $('#RechargeTime').datetimebox('clear');
                    });
                }
            }
        });
        //当充值金额输入数字时，卡内金额变动=卡内余额+充值金额
        $("#RechargeAmount").numberbox({
            onChange: function (newValue, oldValue) {               
                var x = $('#Remaining').numberbox('getText');
                var y = $('#RechargeAmount').numberbox('getText');
                $('#CardAmount').numberbox('setValue', parseFloat(x) + parseFloat(y));
            }
        })

        //当卡内余额输入数字时，卡内金额变动=卡内余额+充值金额
        $("#Remaining").numberbox({
            onChange: function (newValue, oldValue) {
                var x = $('#Remaining').numberbox('getText');
                var y = $('#RechargeAmount').numberbox('getText');
                $('#CardAmount').numberbox('setValue', parseFloat(x) + parseFloat(y));
            }
        })
    }

    //新增方法
    function submitAddForm() {
        var isValid = $("#NewRechargeList").form("validate");
        if (isValid) {
            //$('#btnadd').linkbutton('disable');
            $.messager.confirm('提示', '确认为此油卡充值吗?', function (r) {
                if (r) {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/CM/Recharge/RechargeAdd")',
                        data:
                            {
                                sRechargeInfo: JSON.stringify(RechargeModel(false))
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                $('#NewRechargeList').dialog("close");
                                $("#gridRecharge").datagrid("reload");
                            }, function () {
                                $('#btnadd').linkbutton('enable');
                            });
                        }
                    })
                } else {
                    $('#btnadd').linkbutton('enable');
                }
            });
        } else {
            $.showMsg("请填写必填项！", "提示");
        }
    }
    //修改 post form to edit
    function submitEditForm() {
        $.messager.confirm('提示', '确认修改该数据?', function (r) {
            if (r) {
                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/CM/Recharge/RechargeEdit")',
                    data:
                        {
                            sRechargeInfo: JSON.stringify(RechargeModel(true))
                        },
                    dataType: "json",
                    success: function (jsData) {
                        $.handleJsonResult(jsData, function () {
                            $('#NewRechargeList').dialog("close");
                            $("#gridRecharge").datagrid("reload");
                        });
                    }
                });
            }
        });
    }

    //获取填写所有值
    function RechargeModel(isEdit) {
        var obj = new Object();
        if (isEdit)
            obj.ID = $("#ID").val();
        obj.CardCode = $("#CardCode").combobox("getValue");
        //obj.CenterID = $("#Center").combobox("getValue");
        //obj.StationID = $("#Station").combobox("getValue");
        obj.DriverWorkID = $("#DriverWorkID").combobox('getText');
        obj.DriverName = $("#DriverName").combobox('getText');
        obj.Remaining = $("#Remaining").numberbox('getText');
        obj.RechargeAmount = $("#RechargeAmount").numberbox('getValue');
        obj.CardAmount = $("#CardAmount").numberbox('getValue');
        obj.RechargeTime = $("#RechargeTime").datetimebox('getText');
        obj.FPCode = $("#FPCode").textbox('getText');
        obj.Remark = $("#Remark").textbox('getText');
        obj.FillPerson = '@ViewData["name"]';
        obj.FillTime = '@ViewData["time"]';
        return obj;
    }
    //新增入口
    function initAddInfo() {
        //将修改按钮禁用
        $('#btnedit').linkbutton('disable');
        $.GID("btnedit").style.display = "none";
        $('#btnadd').linkbutton('enable');
        $.GID("btnadd").style.display = "block";
        $("#ID").val("");
        //$("#Center").combobox('setText',"");
        //$("#Station").combobox("setText", "");
        $("#CardCode").combobox("setValue", "");
        $("#Remaining").numberbox('setValue', "");
        $("#Remaining").numberbox({ disabled: true });
        $("#RechargeAmount").numberbox('setValue',"");
        $("#CardAmount").numberbox('setValue',"");
        $("#FPCode").textbox('clear');
        $("#Remark").textbox('clear');
        $("#RechargeTime").datetimebox('setValue', "");
        //$.InitControlDateTime("#RechargeTime");
    }
    function initUpdateInfo() {
        $('#btnadd').linkbutton('disable');
        $.GID("btnadd").style.display = "none";
        $('#btnedit').linkbutton('enable');
        $.GID("btnedit").style.display = "block";
    }
</script>

<div id="NewRechargeList" class="easyui-dialog" title="油卡充值新增/修改页面" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cWuZiWeiHu'" style="width:720px;height:285px;padding:10px;">
    <input id="ID" type="hidden">
    <table border="0" cellpadding="0" cellspacing="8" style="width: 99%; margin-left: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px">
        <tr>
            <td style="width: 100px; text-align: right">
                卡号：
            </td>
            <td>
                <input class="easyui-combobox" style="width: 160px" id="CardCode" data-options="required:true">
            </td>
            <td style="width:100px;text-align:right">
                分中心：
            </td>
            <td>
                <input class="easyui-combobox" style="width: 160px" id="Center" data-options="required:true,editable:false">
            </td>
        </tr>
        <tr>
            <td style="width: 100px; text-align: right">
                油卡负责人工号：
            </td>
            <td>
                <input class="easyui-combobox" style="width: 160px" id="DriverWorkID" data-options="required:true,editable:false">
            </td>
            <td style="width:110px;text-align:right">
                分站：
            </td>
            <td>
                <input class="easyui-combobox" style="width: 160px" id="Station" data-options="editable:false">
            </td>
        </tr>
        <tr>
            <td style="width: 100px; text-align: right">
                油卡负责人姓名：
            </td>
            <td>
                <input class="easyui-combobox" style="width: 160px" id="DriverName" data-options="required:true,editable:false">
            </td>
            <td style="width: 110px; text-align: right">
                卡内余额：
            </td>
            <td>
                <input class="easyui-numberbox" style="width: 160px" id="Remaining" data-options=" disabled:true , precision: 2">
            </td>
        </tr>
        <tr>
            <td style="width: 110px; text-align: right">
                充值日期：
            </td>
            <td>
                <input id="RechargeTime" class="easyui-datetimebox" style="width:160px;" data-options="required:true">
            </td>
            <td style="width: 100px; text-align: right">
                充值金额：
            </td>
            <td>
                <input class="easyui-numberbox" style="width: 160px" id="RechargeAmount" data-options="required:true,min:0,precision: 2">
            </td>
        </tr>
        <tr>
            <td style="width: 110px; text-align: right">
                充值发票编号：
            </td>
            <td>
                <input class="easyui-textbox" style="width: 160px" id="FPCode">
            </td>

            <td style="width: 110px; text-align: right">
                卡内金额：
            </td>
            <td>
                <input id="CardAmount" class="easyui-numberbox" style="width:160px;" data-options="disabled:true,precision: 2">
            </td>
        </tr>
        <tr>
            <td style="width: 100px; text-align: right">备注：</td>
            <td colspan="3">
                <input id="Remark" class="easyui-textbox" data-options="multiline:true" style="width:496px;height:30px">
            </td>
        </tr>
        <tr></tr>
        <tr>
            <td colspan="4" style="text-align:right">
                <a href="javascript:void(0)" id="btnedit" class="easyui-linkbutton" style="width:100px;margin: auto" data-options="iconCls:'icon-edit'" onclick="submitEditForm();">确认修改</a>
                <a href="javascript:void(0)" id="btnadd" class="easyui-linkbutton" style="width:100px;margin: auto" data-options="iconCls:'icon-add'" onclick="submitAddForm();">确认新增</a>
            </td>
        </tr>
    </table>
</div>